<?php

namespace App\Services\Cache;

use App\Http\Controllers\Api\V1\Queries\HotProductQuery;
use App\Http\Controllers\Api\V1\Queries\GroupsProductQuery;
use App\Models\Category;
use App\Models\Product;
use App\Models\Search;
use App\Models\SeckillProduct;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Api\V1\Queries\ProductQuery;

class ProductCache
{
	public function products(Category $category = null,$userId = 0)
	{
		$key = url()->full();

		$products = Cache::remember($key, 1, function () use ($category) {
			$query = app(ProductQuery::class)->sale()->where('is_seckill',false);
			if(request('pop')){

				$query = $query->where('is_pop',true);
			}

			if(isset(request()->user()->id)){
				$query = $query->where('command_user_id',request()->user()->id);
			}
			if ($category and $category->id > 0) {

				if ($category->children()->exists()) {
					$childrenIds = $category->children()->pluck('id');
					$products = $query->whereIn('category_id', $childrenIds)->inRandomOrder()->paginate();
				} else {
					$products = $query->where('category_id', $category->id)->inRandomOrder()->paginate();
			
					
				}
			}else{
				$products = $query->inRandomOrder()->paginate();
				
			}
			
			
			
			return $products;
		});
		return $products;
	}


	public function groupProducts(Category $category = null,$userId = 0)
	{
		$key = url()->full();

		$products = Cache::remember($key, 1, function () use ($category) {
			$query = app(ProductQuery::class)->sale()->group();
			if(request('pop')){

				$query = $query->where('is_pop',true);
			}

			if(isset(request()->user()->id)){
				$query = $query->where('command_user_id',request()->user()->id);
			}
			if ($category and $category->id > 0) {

				if ($category->children()->exists()) {
					$childrenIds = $category->children()->pluck('id');
					$products = $query->whereIn('category_id', $childrenIds);
				} else {
					$products = $query->where('category_id', $category->id);
			
					
				}
			}else{
				$products = $query;
				
			}

			if(request('keyword')){
				$keyword = request('keyword');
				$products = $products->where('title_long', 'like', "%$keyword%");
				Search::recordSearchToCache(request('keyword'));
			}
			
			if(request('new')){
				$products  = $products->orderByDesc('id')->paginate();
			}elseif(request('price')){
				if(request('price')  == 1){

					$products  = $products->orderBy('price_lowest')->paginate();
				}elseif(request('price')  == 2){

					$products  = $products->orderByDesc('price_lowest')->paginate();
				}
			}elseif(request('sold')){
				$products  = $products->orderByDesc('sold_count')->paginate();
			}elseif(request('rate')){

				if(request('rate')  == 1){

					$products  = $products->orderBy('group_number')->paginate();
				}elseif(request('rate')  == 2){

					$products  = $products->orderByDesc('group_number')->paginate();
				}
				//$products  = $products->orderByDesc('reward_rate')->paginate();
			}else{

				$products  = $products->inRandomOrder()->paginate();
			}
			
			return $products;
		});
		return $products;
	}
	
	public function product($id)
	{
		$key = url()->full();
		$product = Cache::remember($key, 1, function () use ($id) {
			return app(ProductQuery::class)->findOrFail($id);
		});
		return $product;
	}
	
	public function drinks()
	{
		$key = url()->full();
		$hots = Cache::remember($key, 1, function () {
			return Product::query()
				->drink()
				->orderByDesc('id')
				->paginate();
		});
		return $hots;
	}

	public function search()
	{
		$keyword = request('keyword');
		$products =  app(ProductQuery::class)->sale()->where('title', 'like', $keyword)->paginate();
		return $products;
	}

	public function displaces($product_id)
	{
		$key = url()->full();

		$money = Product::find($product_id)->price_market;
		$groups = Cache::remember($key, 1, function () use($money){
			return Product::query()->with("skus")
				->join('group_products','products.id','group_products.product_id')
				->where('products.price_market',$money)
				->where('group_products.is_enable',true)
				->orderByDesc('group_products.order')
				->select('products.*','group_products.order','group_products.is_enable')
				->paginate();
		});

		return $groups;
	}
}
